package com.chzu.controller;

import com.chzu.pojo.PoliticalQuota;
import com.chzu.pojo.SemesterScore;
import com.chzu.pojo.SignSubmission;
import com.chzu.result.JsonResult;
import com.chzu.service.PoliticalQuotaService;
import com.chzu.service.SemesterScoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName PoliticalQuotaController
 * @Description TODO
 * @Date 2022/02/10 23:42
 */
@RestController
@RequestMapping("/political")
@Api(tags = "每学期的入党指标")
public class PoliticalQuotaController {

    @Resource
    private PoliticalQuotaService politicalQuotaService;
    @Resource
    private SemesterScoreService semesterScoreService;


    @ApiOperation("添加每学期的入党指标信息")
    @PostMapping("/")
    public JsonResult<Object> addPoliticalQuota(@RequestBody PoliticalQuota politicalQuota) {
        String semester = politicalQuota.getSemester();
        List<SemesterScore> allSemester = semesterScoreService.getAllSemester();
        for(SemesterScore s : allSemester) {
            if (semester.equals(s.getSemester())) {
                return JsonResult.fail("不能添加重复的学期");
            }
        }
        politicalQuota.setCreateTime(new Date());
        boolean res = politicalQuotaService.save(politicalQuota);
        if(!res) {
            return JsonResult.fail("添加每学期的入党指标信息失败");
        }
        return JsonResult.ok("添加每学期的入党指标信息成功");
    }

    @ApiOperation("删除每学期的入党指标信息")
    @Transactional(rollbackFor= Exception.class)
    @DeleteMapping("/{id}")
    public JsonResult<String> deletePoliticalQuota(@PathVariable("id") Long id) {
        boolean res = politicalQuotaService.deletePoliticalQuota(id);
        if (!res) {
            return JsonResult.fail("删除每学期的入党指标信息失败");
        }
        return JsonResult.ok("删除每学期的入党指标信息成功");
    }

    @ApiOperation("修改每学期的入党指标信息")
    @Transactional(rollbackFor = Exception.class)
    @PutMapping("/")
    public JsonResult<String> updatePoliticalQuota(@RequestBody PoliticalQuota politicalQuota) {
        boolean res = politicalQuotaService.updateById(politicalQuota);
        if (!res) {
            return JsonResult.fail("修改每学期的入党指标信息失败");
        }
        return JsonResult.ok("修改每学期的入党指标信息成功");
    }

    @ApiOperation("获取所有每学期的入党指标")
    @GetMapping("/{semester}")
    public JsonResult<List<PoliticalQuota>> getPoliticalQuota(@PathVariable("semester") String semester) {
        List<PoliticalQuota> data = politicalQuotaService.getPoliticalQuota(semester);
        return JsonResult.ok(data);
    }

    @ApiOperation("获取所有的学期")
    @GetMapping("/getSemesters")
    public JsonResult<List<PoliticalQuota>> getSemesters() {
        List<PoliticalQuota> list = politicalQuotaService.list();
        return JsonResult.ok(list);
    }
}
